<template>
  <div class="content">
    <el-form :inline="true" :model="formInline">
      <el-form-item label="用户账号">
        <el-input v-model="formInline.accountSearch" placeholder="请输入用户账号" clearable />
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="formInline.userNameSearch" placeholder="请输入用户昵称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button type="primary" @click="dialogVisible=true" plain>新增用户</el-button>
        <el-button @click="init">重置</el-button>
        <el-button @click="delFn" type="danger" :disabled="delParams.ids.length==0">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData.records" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" />
      <el-table-column prop="account" label="账号" align="center" />
      <el-table-column prop="realName" label="昵称" align="center" />
      <el-table-column prop="des" label="备注" align="center" />
      <el-table-column label="状态" align="center" min-width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.enableStatus==1">正在使用</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户类型" align="center" min-width="150">
        <template #default="scope">
          {{scope.row.userType==0?"普通管理员":"超级管理员"}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="170" align="center">
        <template #default="scope">
          {{$utils.dateTimeFormateFn(scope.row.createTimeLong,"all")}}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" min-width="170" align="center">
        <template #default="scope">
          {{$utils.dateTimeFormateFn(scope.row.updateTimeLong,"all")}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" min-width="200">
        <template #default="scope">
          <el-button type="primary" text @click="handleClick(scope.row,1)" icon="Edit">编辑</el-button>
          <el-button type="primary" text @click="handleClick(scope.row,2)" icon="Delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-if="tableData['total']" :total="tableData['total']" v-model:page="formInline.pageNum" v-model:limit="formInline.pageSize" @pagination="getInfo" />

    <el-dialog v-model="dialogVisible" title="新增/编辑账号" width="600" :before-close="handleClose">
      <el-form label-width="100" :model="form" :rules="rules" ref="formEl">
        <el-form-item label="用户账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入用户账号" clearable />
        </el-form-item>
        <el-form-item label="用户名称" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入用户名称" clearable />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="form.password" type="password" auto-complete="off" placeholder="密码" show-password />
        </el-form-item>
        <el-form-item label="用户类型">
          <el-radio-group v-model="form.userType">
            <el-radio :label="0">普通用户</el-radio>
            <el-radio :label="1">超级管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="使用状态">
          <el-switch v-model="form.enableStatus" :active-value="1" :inactive-value="2" />
        </el-form-item>
        <el-form-item label="角色">
          <selectRole v-model="form.roleIds"></selectRole>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.des" placeholder="请输入备注" type="textarea" :autosize="{ minRows : 4 }" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            确 认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import selectRole from "@/components/SystemMain/selectRole.vue";
const { proxy } = getCurrentInstance();

let dialogVisible = ref(false);
let tableData = ref({});
let formInline = ref({
  pageNum: 1,
  pageSize: 10,
  accountSearch: null,
  userNameSearch: null,
});
let loading = ref(false);
let delParams = ref({
  ids: [],
});
let form = ref({
  account: "",
  des: "",
  enableStatus: 1,
  password: "",
  realName: "",
  roleIds: [],
  userType: 0,
});
const rules = reactive<FormRules<RuleForm>>({
  account: [
    {
      required: true,
      message: "请输入账号",
      trigger: "blur",
    },
  ],
  realName: [{ required: true, message: "请输入名称", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

onMounted(() => {
  search();
});

let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let getInfo = async () => {
  tableData.value = await proxy.$apis.users(formInline.value);
};
let init = () => {
  formInline.value = {
    pageNum: 1,
    pageSize: 10,
    accountSearch: null,
    userNameSearch: null,
  };
  getInfo();
};
let handleSelectionChange = (e) => {
  let ids = [];
  e.forEach((element) => {
    ids.push(element.id);
  });
  delParams.value.ids = [...ids];
};

let delFn = async () => {
  let resp = await proxy.$utils.confirmTips("是否确定删除?");
  if (!resp) return;
  await proxy.$apis.userDel(delParams.value);
  proxy.$utils.showToast("删除成功");
  getInfo();
};

let handleClick = async (row, num: Number) => {
  switch (num) {
    case 1:
      let resp = await proxy.$apis.userDetails({ id: row.id });
      form.value = proxy.$utils.formatFunction(resp, { ...form.value });
      form.value.id = row.id;
      dialogVisible.value = true;
      break;
    case 2:
      delParams.value.ids = [row.id];
      delFn();
      break;
    default:
      break;
  }
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!proxy.$refs.formEl) return;
  await proxy.$refs.formEl.validate((valid, fields) => {
    if (valid) {
      save();
    } else {
      console.log("error submit!", fields);
    }
  });
};
let save = async () => {
  try {
    loading.value = true;
    if (form.value.id) {
      await proxy.$apis.userUpdate(form.value);
    } else {
      await proxy.$apis.userAdd(form.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};

let handleClose = () => {
  form.value = {
    account: "",
    des: "",
    enableStatus: 1,
    password: "",
    realName: "",
    roleIds: [],
    userType: 0,
  };
  proxy.$refs.formEl.resetFields();
  getInfo();
  dialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
.content {
  margin: 20px;
}
</style>
